package com.maila.xiaoaoschool.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.maila.xiaoaoschool.R;
import com.maila.xiaoaoschool.entity.FoodEntity;
import com.maila.xiaoaoschool.entity.ShopClasses;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/9/26.
 */

public class ShopMunuAdapter extends BaseAdapter {
    Context context;
    List<ShopClasses.DataBean> mdata;
    int mIndex=0;
    public ShopMunuAdapter(Context context) {
        this.context = context;
        mdata=new ArrayList<>();
    }

    public List<ShopClasses.DataBean> getMdata() {
        return mdata;
    }

    public void setMdata(List<ShopClasses.DataBean> mdata) {
        this.mdata = mdata;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mdata.size();
    }

    @Override
    public ShopClasses.DataBean getItem(int i) {
        return mdata.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_menu, viewGroup, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
            AutoUtils.autoSize(view);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        if (i == mIndex) {
            view.setBackgroundColor(context.getResources().getColor(R.color.white));
        } else {
            view.setBackgroundColor(context.getResources().getColor(R.color.gray));
        }
        holder.itemName.setText(mdata.get(i).getTypename());
        if(mdata.get(i).getTypename().contains("热销")){
            holder.ivSelect.setVisibility(View.VISIBLE);
            holder.ivSelect.setImageResource(R.mipmap.rexiao);
        }else if(mdata.get(i).getTypename().contains("打折")|mdata.get(i).getTypename().contains("促销")){
            holder.ivSelect.setVisibility(View.VISIBLE);
            holder.ivSelect.setImageResource(R.mipmap.dazhecuxiao);
        }else {
            holder.ivSelect.setVisibility(View.GONE);
        }

        return view;
    }

    /**
     * 设置当前的位置
     *
     * @param mIndex
     */
    public void setIndex(int mIndex) {
        this.mIndex = mIndex;
        notifyDataSetChanged();
    }
    static class ViewHolder {
        @BindView(R.id.iv_select)
        ImageView ivSelect;
        @BindView(R.id.item_name)
        TextView itemName;
        @BindView(R.id.bg_item)
        AutoLinearLayout bgItem;
        @BindView(R.id.tv_nm)
        TextView tvNm;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
